<?php
require('includes/top.php');

if($_POST['indnew']==3 || $_POST['indnew']==4)
{

if(!isset($_POST['addgrpemp']))
{
$sizeof = 0;
}
else
{
$sizeof = $_POST['addgrpemp'];
}


for ($i = 0; $i <=$sizeof; $i++) {

$empno = $_POST["empno".$i.""];
$emp_fname = $_POST["emp_fname".$i.""];
$emp_lname = $_POST["emp_lname".$i.""];
$emp_desn = $_POST["emp_desn".$i.""];
$emp_telno = $_POST["emp_telno".$i.""];
$emp_address = $_POST["emp_address".$i.""];
$emp_email = $_POST["emp_email".$i.""];
$emp_password = $_POST["empno".$i.""];
$emp_name = $_POST["empno".$i.""];

$division = $_POST["division"];
$region = $_POST["region"];

if($_POST["laptop_model"]=="0")
{
$laptop_model = $_POST["laptop_model1"];
}
else
{
$laptop_model = $_POST["laptop_model"];
}

for ($i = 0; $i <count($_POST[acc_data]); $i++) {
$laptop_acc .= $_POST["laptop_acc"][$i];
}

foreach($laptop_acc as $data)
{
$a .=  $data.',';
}


$laptop_acc = $a;

$depot = $_POST["depot"];
$dispatch_location = $_POST["dispatch_location"];
$dispatch_date = $_POST["doc3"];

$emp_oldno = $_POST["emp_oldno".$i.""];

$query = "SELECT * FROM users WHERE `user_no` = '".$empno."'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result )> 0 )
{
    header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
}
else
 {
 if($emp_oldno=="") 
{ 
$sql="INSERT INTO users(`user_groups_id`,`user_no`,`user_firstname`,`user_lastname`,`user_telno`,`user_address`,`user_email_address`,`user_password`,`user_name`,`user_designation`) VALUES ('1','$empno','$emp_fname','$emp_lname','$emp_telno','$emp_address','$emp_email','$empno','$empno','$emp_desn')";
 $result=mysql_query($sql);




// recipients
$to  = $emp_email; // 
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p><strong>Following is your Login Details:</strong></p>
  <table>
    <tr>
      <td>Username:</td><td>'.$empno.'</td>
    </tr>
    <tr>
      <td>Password:</td><td>'.$empno.'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: no-reply@laptrak.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);


 
 
$user_id = mysql_insert_id();



$modelname_array = explode('-',$laptop_model);

$sql8 = "select min(laptop_id) from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);
$serial_no = $row10[0];

$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);



$sql1="INSERT INTO laptop(`user_id`,`division`,`region`,`laptopmodel`,`depot`,`dispatch_location`,`dispatch_date`,`acc`,`serial_no`) VALUES ('$user_id','$division','$region','$laptop_model','$depot','$dispatch_location','$dispatch_date','$laptop_acc','$serial_no')";

$result1=mysql_query($sql1);



if($result!=1)
{
echo $sql;
exit;
 header("location:".$_REQUEST['PageName']."&aexit=1");
}
 
 }
 else
{
$query1 = "SELECT user_id FROM users WHERE `user_no` = '{$emp_oldno}'";
$result2 = mysql_query($query1);
if (mysql_num_rows ( $result2)!=1)
{
    header("location:".$_REQUEST['PageName']."&nexit=1");
	die();
}
else
{
$row = mysql_fetch_row($result2);
$uno = $row[0];



$modelname_array = explode('-',$laptop_model);

$sql8 = "select min(laptop_id) from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);


$serial_no = $row10[0];


$sql1="INSERT INTO laptop(`user_id`,`division`,`region`,`laptopmodel`,`depot`,`dispatch_location`,`dispatch_date`,`acc`,`serial_no`) VALUES ('$uno','$division','$region','$laptop_model','$depot','$dispatch_location','$dispatch_date','$laptop_acc','$serial_no')";

$result3=mysql_query($sql1);

}
}
 
}
header("location:".$_REQUEST['PageName']."&v=success");
}
}
else
{
$empno = $_POST["empno"];
$emp_fname = $_POST["emp_fname"];
$emp_lname = $_POST["emp_lname"];
$emp_desn = $_POST["emp_desn"];
$emp_telno = $_POST["emp_telno"];
$emp_address = $_POST["emp_address"];
$emp_email = $_POST["emp_email"];
$emp_password = $_POST["empno"];
$emp_name = $_POST["empno"];


$division = $_POST["division"];
$region = $_POST["region"];
if($_POST["laptop_model"]=="0")
{
$laptop_model = $_POST["laptop_model1"];
}
else
{
$laptop_model = $_POST["laptop_model"];
}


for ($i = 0; $i <count($_POST[acc_data]); $i++) {
$laptop_acc .= $_POST["laptop_acc"][$i];
}

foreach($laptop_acc as $data)
{
$a .=  $data.',';
}


$laptop_acc = $a;

$depot = $_POST["depot"];
$dispatch_location = $_POST["dispatch_location"];
$dispatch_date = $_POST["doc3"];

$emp_oldno = $_POST["emp_oldno"];



$query = "SELECT * FROM users WHERE `user_no` = '{$empno}'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result )> 0 )
{
    header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
}
else
 {
 
 
if($emp_oldno=="") 
{ 
 $sql="INSERT INTO users(`user_groups_id`,`user_no`,`user_firstname`,`user_lastname`,`user_telno`,`user_address`,`user_email_address`,`user_password`,`user_name`,`user_designation`) VALUES ('1','$empno','$emp_fname','$emp_lname','$emp_telno','$emp_address','$emp_email','$empno','$empno','$emp_desn')";

$result=mysql_query($sql);





// recipients
$to  = $emp_email; // 
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p><strong>Following is your Login Details:</strong></p>
  <table>
    <tr>
      <td>Username:</td><td>'.$empno.'</td>
    </tr>
    <tr>
      <td>Password:</td><td>'.$empno.'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: no-reply@laptrak.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);



$user_id = mysql_insert_id();




$modelname_array = explode('-',$laptop_model);

$sql8 = "select min(laptop_id) from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);

$serial_no = $row10[0];

$sql1="INSERT INTO laptop(`user_id`,`division`,`region`,`laptopmodel`,`depot`,`dispatch_location`,`dispatch_date`,`acc`,`serial_no`) VALUES ('$user_id','$division','$region','$laptop_model','$depot','$dispatch_location','$dispatch_date','$laptop_acc','$serial_no')";

$result1=mysql_query($sql1);

if($result!=1)
{
 header("location:".$_REQUEST['PageName']."&aexit=1");
}
}
else
{
$query1 = "SELECT user_id FROM users WHERE `user_no` = '{$emp_oldno}'";
$result2 = mysql_query($query1);
if (mysql_num_rows ( $result2)!=1)
{
    header("location:".$_REQUEST['PageName']."&nexit=1");
	die();
}
else
{
$row = mysql_fetch_row($result2);
$uno = $row[0];



$modelname_array = explode('-',$laptop_model);

$sql8 = "select min(laptop_id) from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);


$serial_no = $row10[0];

$sql1="INSERT INTO laptop(`user_id`,`division`,`region`,`laptopmodel`,`depot`,`dispatch_location`,`dispatch_date`,`acc`,`serial_no`) VALUES ('$uno','$division','$region','$laptop_model','$depot','$dispatch_location','$dispatch_date','$laptop_acc','$serial_no')";

$result3=mysql_query($sql1);
}
}


}




header("location:".$_REQUEST['PageName']."&v=success");
}
?>
